#! /usr/bin/env python2.7
"""Test launcher script."""
import os
import os.path
import sys
from unittest import loader
from unittest import runner


# Poor-man's `which` command.
for path in os.environ['PATH'].split(':'):
    if 'appcfg.py' in os.listdir(path):
        break
else:
    raise RuntimeError("couldn't find appcfg.py on $PATH")

# Find out where App Engine lives so we can import it.
app_engine_path = os.path.join(os.path.dirname(path), 'google_appengine')
if not os.path.isdir(app_engine_path):
    raise RuntimeError('{} does not exist'.format(app_engine_path))
sys.path.append(app_engine_path)

# Because life would be too simple if third-party dependencies were structured
# in a reliable way.
import appcfg
sys.path.extend(appcfg.EXTRA_PATHS)

# Run all tests in the current directory.
loader = loader.TestLoader()
tests = loader.discover('.')
runner = runner.TextTestRunner()
result = runner.run(tests)
sys.exit(result.wasSuccessful())
